/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"

void WR_information_block(unsigned char addr ,unsigned char dataH ,unsigned char dataL );   // addr = 0x00~0x17
unsigned int RE_information_block(unsigned char addrL);	// addr = 0x00~0x17

unsigned int RE_flash(addrH,addrL);

/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay( void )
{
	unsigned int i;
	
	for( i = 0 ; i < 200 ; i++ ){}
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main( void )
{	
	unsigned int RE_data1,RE_data2,RE_data3,RE_data4;
	
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();
	
/* Set PD1. ,PD2.,PD3 output */
	PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );
	
	PD1 = 0; // LED_G1 On
	PD2 = 1 ; //LED_R Off
	PD3= 0;  // LED_G2  On
	
	/* enable FMC clock */
	En_FMC_clk();
	
	WR_information_block(0x00,0x88,0x89);
	RE_data1 = RE_information_block(0x00);
	
	WR_information_block(0x01,0x90,0x91);
	RE_data2 = RE_information_block(0x01);
	
	WR_information_block(0x17,0x32,0x33);
	RE_data3 = RE_information_block(0x17);
	
	//Read Falsh
	RE_data4 = RE_flash(0x0E,0x00);
	
	
	
	delay();
	
	while(1);
}

void WR_information_block(unsigned char addr,unsigned char dataH ,unsigned char dataL)// addr = 0x00~0x17
{
		/* enable information block */
		Enable_information_block();                // ISPEN = 0, INFOEN = 1, ISAVB = 1, ISPWDTD = 0
		
		/* Write eMTP information block */
		FMC->fmcadr[0]  = addr;
		FMC->fmcadr[1]  = 0x00;
		FMC->fmcdata[0] = dataL;
		FMC->fmcdata[1] = dataH;
		FMC->fmccmd     = 0x08;
		while((FMC->fmcstat & 0x80) == 0x80){
		}
	
		/* disable information block */
		Disable_information_block();                // ISPEN = 0, INFOEN = 0, ISAVB = 1, ISPWDTD = 0
	
}

unsigned int RE_information_block(unsigned char addrL)	// addrL = 0x00~0x17
{
		int infodata;
		unsigned char data0, data1;
		/* enable information block */
		Enable_information_block();                // ISPEN = 0, INFOEN = 1, ISAVB = 1, ISPWDTD = 0
		
		FMC->fmcadr[0] = addrL;
		FMC->fmcadr[1] = 0x00;
		FMC->fmccmd    = 0x02;
		data1          = FMC->fmcdata[1];
		FMC->fmccmd    = 0x02;
		data0          = FMC->fmcdata[0];
		infodata       = (data1 << 8) | data0;
		
		/* disable information block */
		Disable_information_block();                // ISPEN = 0, INFOEN = 0, ISAVB = 1, ISPWDTD = 0
		return infodata;
	
}
unsigned int RE_flash(addrH,addrL)
{
		int infodata;
		unsigned char data0, data1;
	
		/* disable information block */
		Disable_information_block();                // ISPEN = 0, INFOEN = 0, ISAVB = 1, ISPWDTD = 0
		
		FMC->fmcadr[0] = addrL;
		FMC->fmcadr[1] = addrH;
		FMC->fmccmd    = 0x02;
		data1          = FMC->fmcdata[1];
		FMC->fmccmd    = 0x02;
		data0          = FMC->fmcdata[0];
		infodata       = (data1 << 8) | data0;
		
		return infodata;
}
